package auth

import (
	"MembershipRegist/controllers/ctrl"
	"MembershipRegist/db"
	"MembershipRegist/models"
	"errors"
)

type UserController struct {
	ctrl.Controller
	User   *models.User
	UserID uint
}

var (
	ErrUserNotLogin = errors.New("user not login")
)

func (u *UserController) Prepare() {
	userID := u.GetSession("user_id")
	if userID == nil {
		u.Abort("403")
		return
	}
	var user models.User
	if value := db.DB.Where("id = ?", userID.(uint)).First(&user); value.Error != nil {
		u.Abort("403")
		return
	}
	u.User = &user
	u.UserID = user.ID
	return
}

func (u *UserController) UserInfo() {
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["user"] = *u.User
	u.Data["json"] = result
	u.ServeJSON()
}
